/**
Defines global constants and functions.
*/

#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include "unp.h"

#define PORT_NUM_ECHO 9000
#define PORT_NUM_TIME 9001

#define PORT_NUM_ECHO_STR "9000"
#define PORT_NUM_TIME_STR "9001"

#define log_msg(message) printf("[UNP LOG(%s:%d)] %s\n", __FILE__, __LINE__,\
message)
#define log_msg_str(arg, message) printf("[UNP LOG(%s:%d)] %s: %s\n",\
__FILE__, __LINE__, arg, message)
#define log_msg_int(value) printf("[UNP LOG(%s:%d)] value: %d\n", __FILE__,\
__LINE__,value)


#define log_server(msg) printf("[DAYTIMESRV(%s:%d)] [DIAGNOSTIC_MESSAGE]%s\n",\
__FILE__, __LINE__, msg);

#define log_server_error(msg, errno) printf("[DAYTIMESRV(%s:%d)] \
[DIAGNOSTIC_ERORR_MESSAGE]%s\n",__FILE__, __LINE__, msg, errno);


#define itoa(num, bufr) sprintf(bufr,"%d", num)

#define TRUE 1

#define MENU_STR "Choose the service. Type:\n[echo] for echo\n[time] for time\
\n[quit] to quit\n"

#define MENU_ITEM_ECHO "echo"
#define MENU_ITEM_TIME "time"
#define MENU_ITEM_QUIT "quit"

#define MENU_ITEM_LENGTH strlen(MENU_ITEM_ECHO)

#define DIAG_ECHO_CLIENT_CONN "Echo client connected to server.\n"
#define DIAG_ECHO_CLIENT_CONN_ERR "Echo client error: Could not connect to\
 the server.\n"
#define DIAG_ECHO_CLIENT_TERM "[DIAGNOSTIC MESSAGE] Echo client terminated.\n"
#define DIAG_ECHO_CLIENT_RCVD "[DIAGNOSTIC MESSAGE] Echo client says: "

#define DIAG_TIME_CLIENT_CONN "Daytime client connected to server.\n"
#define DIAG_TIME_CLIENT_CONN_ERR "Time client error: Could not connect to\
 the server.\n"
#define DIAG_TIME_CLIENT_TERM "[DIAGNOSTIC MESSAGE] Daytime client terminated.\n"
#define DIAG_TIME_CLIENT_RCVD "[DIAGNOSTIC MESSAGE] Daytime client says: "

#define DIAG_TIME_SRV_TERM "Daytime client termination. Server detected client\
 FIN. Socket read returned with value 0."
#define DIAG_ECHO_SRV_TERM "Echo client termination. Server detected client\
 FIN. Socket read returned with value 0."

#define DIAG_TIME_SRV_TERM_ERR "Daytime client termination. Socket read \
returned error:"
#define DIAG_ECHO_SRV_TERM_ERR "Echo client termination. Socket read \
returned error:"

#define DIAG_ECHO_SRV_TERM_EPIPE "Echo client termination. Socket write\
 returned EPIPE."
#define DIAG_TIME_SRV_TERM_EPIPE "Daytime client termination. Socket write\
 returned EPIPE."

#define SRV_PREMATURE_TERM "Server terminated prematurely.\n"

void sig_chld(int signo);
void str_cli_ext(FILE* fp, int sockfd, int pfd);
char* get_ip_addr(char *input);
void str_echo_ext(int);

void exec_time_client(int pfd[2], char* ip_addr);
void exec_echo_client(int pfd[2], char* ip_addr);

void* echo_thread_func(void* arg);
void* time_thread_func(void* arg);

ssize_t ts_readline(int fd, void *vptr, size_t maxlen);

ssize_t writen_ext(int fd, const void *vptr, size_t n);

int Tcp_listen_ext(const char *host, const char *serv, socklen_t *addrlenp);

#endif
